package so.sao.code.service.domain.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import so.sao.code.service.domain.entity.RangeActivation;
import so.sao.code.service.domain.entity.TagOperationLog;
import so.sao.code.service.domain.entity.TagTask;
import so.sao.code.service.domain.event.RangeActivationActiveEvent;
import so.sao.code.service.domain.service.RangeActivativeDomainService;
import so.sao.code.service.domain.service.TagBatchDomainService;
import so.sao.code.service.infrastructure.SessionContext;
import so.sao.code.service.infrastructure.cache.CacheManager;
import so.sao.code.service.infrastructure.context.TaskContext;
import so.sao.code.service.repository.TagOperationLogDao;
import so.sao.code.service.repository.TagTaskDao;
import so.sao.ffw.dto.message.TagRangeActivationMessage;
import so.sao.nosql.datastore.enums.tag.TagStatus;
import so.sao.sso.feign.dto.UserDTO;
import so.sao.top.core.util.mapper.JsonMapper;

/**
 * Created by Administrator on 2017/9/13.
 */
@Component
public class RangeActiveationHandler {
    @Autowired
    TagOperationLogDao tagOperationLogDao;
    @Autowired
    TagBatchDomainService tagBatchDomainService;
    @Autowired
    TagTaskDao tagTaskDao;
    @Autowired
    SessionContext context;
    @Autowired
    RangeActivativeDomainService rangeActivativeDomainService;

    @Autowired
    JsonMapper jsonMapper;


    @EventListener
    @Order(1)
    public void a_saveTagTask(RangeActivationActiveEvent event) {
        TagTask tagTask = new TagTask(event.getRangeActivation().GenUniqueKey(), RangeActivation.class.toString(), jsonMapper.toJson(event.getRangeActivation()));
        CacheManager.cache.put(event.getRangeActivation().GenUniqueKey(), tagTask);
        tagTaskDao.save(tagTask);
    }

//    @Order(2)
    //@EventListener(condition = "#event.isActive()")
//    @EventListener
    public void setTagBatchActiveStatus(RangeActivationActiveEvent event) {
        //tagBatchDomainService.setTagBatchActiveStatus(event.getRangeActivation(), event.getTagBatch());
    }

    @Order(3)
    @EventListener
    //@Async
    public void b_activeToFFW(RangeActivationActiveEvent event) {
        Object tagTask = CacheManager.cache.get(event.getRangeActivation().GenUniqueKey());
        if (tagTask == null) {
            tagTask = tagTaskDao.findByGuid(event.getRangeActivation().GenUniqueKey());
        }
        TagRangeActivationMessage message = rangeActivativeDomainService.createTagRangeActivationMessage(event.getTagBatch(), event.getRangeActivation(), TagStatus.ACTIVATE);
        message.setMessageId(((TagTask) tagTask).getGuid());
        new TaskContext<>(message, (TagTask) tagTask, obj -> rangeActivativeDomainService.activeToCloud(obj)).execute();
        CacheManager.cache.remove(event.getRangeActivation().GenUniqueKey());
    }


    /**
     *
     * */
    @Order(4)
    @EventListener
    @Async
    public void logAction(RangeActivationActiveEvent event) {
        UserDTO user = context.CURRECT_USER;
        RangeActivation rangeActivation = event.getRangeActivation();
        String action = event.getOperation().getName();
        TagOperationLog log = new TagOperationLog(
                rangeActivation.getTagBatchId(),
                "管理端",
                action,
                rangeActivation.getSeqnumStart() + "-" + rangeActivation.getSeqnumEnd(),
                user.getUsername());
        tagOperationLogDao.save(log);
    }
}

